
package com.scs.application.modules.base.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.model.CommonResult;
import com.scs.application.core.model.PageResult;
import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.core.mvc.controller.ImAndExAbleController;
import com.scs.application.core.utils.UserUtils;
import com.scs.application.modules.base.entity.HospitalSupplier;
import com.scs.application.modules.base.entity.PrintTemplate;
import com.scs.application.modules.base.request.HospitalSupplierQueryRequest;
import com.scs.application.modules.base.request.PrintTemplateQueryRequest;
import com.scs.application.modules.base.service.HospitalSupplierService;
import com.scs.application.modules.base.service.PrintTemplateService;
import com.scs.application.modules.base.vo.PrintTemplateVO;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 打印模板配置 controller
 */
@AllArgsConstructor
@RestController
@RequestMapping("/base/print_templates")
public class PrintTemplateController extends CrudController<PrintTemplate, String, PrintTemplateService> implements ImAndExAbleController<PrintTemplateQueryRequest> {

    @Autowired
    private final HospitalSupplierService hospitalSupplierService;

    @GetMapping("/page")
    public PageResult page(PrintTemplateQueryRequest request) {
        IPage page = service.page(request);
        return PageResult.ok("查询成功", page.getRecords(), page.getTotal());
    }

    @GetMapping("/list")
    public CommonResult list(PrintTemplateQueryRequest request) {
        List<PrintTemplateVO> list = service.list(request);
        return CommonResult.ok("查询成功", list);
    }


    /**
     * 查询是否进行开票分组配置
     */
    @GetMapping("/listGroupByDept")
    public CommonResult listGroupByDept() {
        //查询当前供应商供货医院
        List<HospitalSupplier> listHospitals = hospitalSupplierService.list(new HospitalSupplierQueryRequest().setSupplierId(UserUtils.getSupplierId()));
        List<PrintTemplate> list = new ArrayList<>();
        if (listHospitals.size() > 0) {
            List<String> hospitals = new ArrayList<>();
            listHospitals.forEach(o -> hospitals.add(o.getHospitalId()));
            list = service.list(Wrappers.<PrintTemplate>query().eq("bus_type", "invoice").in("hospital_id", hospitals));
        }
        return CommonResult.ok("查询成功", list);
    }
}
